package com.visionblue.business.ship.page.register.config

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.visionblue.business.ship.R
import com.visionblue.business.ship.base.BaseVMFragment
import com.visionblue.business.ship.model.repository.db.MeasureMenuItem
import kotlinx.android.synthetic.main.frag_menu_config.*
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.getViewModel

/**
 * @author: xlh
 * @date: 2020-07-21 4:44 PM
 * @desc:
 */
class MenuConfigFragment : BaseVMFragment<MenuConfigViewModel>() {

    private val mInAdapter = MenuConfigAdapter()
    private val mOutAdapter = MenuConfigAdapter()

    override fun provideVM(): MenuConfigViewModel {
        return getViewModel()
    }

    override val layoutId = R.layout.frag_menu_config

    override fun initView(savedInstanceState: Bundle?) {

        rvIn.adapter = mInAdapter
        rvIn.layoutManager = LinearLayoutManager(context)
        rvOut.adapter = mOutAdapter
        rvOut.layoutManager = LinearLayoutManager(context)

        val listener = object : MenuConfigAdapter.OnItemClickListener {
            override fun onMenuUpdate(measureMenuItem: MeasureMenuItem) {
                mViewModel.updateMenuItem(measureMenuItem)
            }
        }

        mInAdapter.onItemClickListener = listener
        mOutAdapter.onItemClickListener = listener
    }

    override fun initData() {
        super.initData()

        mViewModel.inMenuItemsLiveData.observe(this, Observer {
            mInAdapter.setData(it)
        })

        mViewModel.outMenuItemsLiveData.observe(this, Observer {
            mOutAdapter.setData(it)
        })

        mViewModel.getMenuItems()
    }

    companion object {
        fun newInstance(): MenuConfigFragment {
            return MenuConfigFragment()
        }
    }

}